package ru.spb.osll.myfit.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Vector;

import org.junit.Test;

import ru.spb.osll.myfit.server.parser.PageParser;
import ru.spb.osll.myfit.server.parser.PageParserUtils;

public class PageTest {

	private static final String IMPORT = new String("|Import|" + PageParser.LINE_SEPARATOR
			+ "|ru.spb.osll.myfit.examples|" + PageParser.TABLE_SEPARATOR);

	private static final String TEST_PAGE = new String("|Div|" + PageParser.LINE_SEPARATOR
			+ "|numerator|denominator|quotient?|" + PageParser.LINE_SEPARATOR
			+ "|10|2|5.0|" + PageParser.LINE_SEPARATOR
			+ "|11|2|4<_<6|" + PageParser.LINE_SEPARATOR
			+ "|100|4|33|" + PageParser.TABLE_SEPARATOR
			+ "|verify delete with cubetools using|10|threads fails|" + PageParser.TABLE_SEPARATOR);

	Page m_page = new Page();
	Page m_pageWithContent = createPage();

	public static Vector<TableCell> tableLine(String... args) {
		Vector<TableCell> result = new Vector<TableCell>();
		for (String arg : args) {
			result.add(new TableCell(arg));
		}
		return result;
	}

	public static Page createPage() {
		Page page = new Page();
		Table table = new Table(TableStyle.UNKNOWN_FIXTURE);
		table.addLine(tableLine("Import"));
		table.addLine(tableLine("ru.spb.osll.myfit.examples"));
		page.getElements().add(table);
		table = new Table(TableStyle.COLUMN_FIXTURE);
		table.addLine(tableLine("Div"));
		table.addLine(tableLine("numerator", "denominator", "quotient?"));
		table.addLine(tableLine("10", "2", "5.0"));
		table.addLine(tableLine("11", "2", "4<_<6"));
		table.addLine(tableLine("100", "4", "33"));
		page.getElements().add(table);
		table = new Table(TableStyle.UNKNOWN_FIXTURE);
		table.addLine(tableLine("verify delete with cubetools using", "10",
				"threads fails"));
		page.getElements().add(table);
		return page;
	}

	@Test
	public void testcountPageElements() {
		assertTrue(m_page.countPageElements() == 0);
	}

	public void testInsertElement() {
		m_page.insertElement(new Table(TableStyle.UNKNOWN_FIXTURE), 0);
		m_page.insertElement(new Table(TableStyle.UNKNOWN_FIXTURE), 0);
		assertTrue(m_page.countPageElements() == 2);
	}

	@Test(expected = ArrayIndexOutOfBoundsException.class)
	public void testGetElement() {
		for (int j = 0; j < m_page.countPageElements() + 1; j++) {
			m_page.getElement(j);
			assertFalse(j == m_page.countPageElements());
		}
	}

	@Test
	public void testDeleteElements() {
		m_page.deleteElements();
		assertTrue(m_page.countPageElements() == 0);
	}

	@Test
	public void testToString() {
		assertEquals(IMPORT + TEST_PAGE, PageParserUtils
				.pageToString(m_pageWithContent));
	}
}
